﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Drawing;
using System.Collections.ObjectModel;
using System.Linq.Expressions;

public partial class betaling : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Game> winkelgames = (List<Game>) Session["winkelwagen"];
        List<Game> winkelcovers = (List<Game>) Session["winkelwagenCover"];
        DataClassesDataContext db = new DataClassesDataContext();

        MembershipUser currentUser = Membership.GetUser();

        Winkelwagen wwagen = new Winkelwagen();
        wwagen.BETAALD = "1";
        wwagen.CREDITCARD = 0;
        wwagen.DATUM = DateTime.Now;
        wwagen.IP_ADRES = "123.456.789";
        wwagen.KLANT_ID = int.Parse(currentUser.ProviderUserKey.ToString());

        db.Winkelwagens.InsertOnSubmit(wwagen);
        db.SubmitChanges();
        List<Game> coververzameling = new List<Game>();
        if (winkelgames != null)
        {
            foreach (Game wgame in winkelgames)
            {
                Winkelwagengame wwagenGame = new Winkelwagengame();
                wwagenGame.WINKELWAGEN_ID = wwagen.WINKELWAGEN_ID;
                wwagenGame.GAME_NR = wgame.GAME_NR;
                wwagenGame.GAME_BOOL = 1;

                //check if there is cover
                if (winkelcovers != null)
                {
                    foreach (Game wcover in winkelcovers)
                    {
                        if (wcover.GAME_NR == wgame.GAME_NR)
                        {
                            wwagenGame.AFBEELDING_BOOL = 1;
                            coververzameling.Add(wcover);
                        }
                    }
                }
                db.Winkelwagengames.InsertOnSubmit(wwagenGame);
            }
        }
        foreach (Game delete in coververzameling)
        {
            winkelcovers.Remove(delete);
        }

        if (winkelcovers != null)
        {
            foreach (Game wcover in winkelcovers)
            {
                Winkelwagengame wwagenCover = new Winkelwagengame();
                wwagenCover.WINKELWAGEN_ID = wwagen.WINKELWAGEN_ID;
                wwagenCover.GAME_NR = wcover.GAME_NR;
                wwagenCover.AFBEELDING_BOOL = 1;
                db.Winkelwagengames.InsertOnSubmit(wwagenCover);
            }
        }

        db.SubmitChanges();
        Session.Clear();
        betaal_tekst.Text = "Uw betaling is voldaan";
        
    }
}
